<template>
  <div class="app-container">
    <!--
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:advertisement:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="advertisementList">
      <el-table-column
        label="广告标题"
        align="left"
        prop="title"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="轮播图类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <!--          <dict-tag :options="dict.type.banner_type" :value="scope.row.type"/>-->
          <el-tag v-if="scope.row.type == 0">外部链接</el-tag>
          <el-tag v-if="scope.row.type == 1" type="success">内部链接</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="活动类型" align="center" prop="activityType" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.type == 1">
            <dict-tag :options="dict.type.instation_skip" :value="scope.row.activityType" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" width="680">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">{{ scope.row.activityId }}</span>
          <span v-else>{{ scope.row.hyperlink}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新人"
        align="center"
        prop="updateBy"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">{{$yun.convertTimestamp(scope.row.updateTime)}}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-top"
              @click="handleShow(scope.row)"
              v-hasPermi="['car:advertisement:edit']"
            >立即弹窗</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['car:advertisement:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:advertisement:remove']"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告标题" prop="title">
          <el-input v-model="form.title" type="text" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="外链" prop>
          <el-radio-group v-model="radio" @input="changeRadio">
            <el-radio :label="0">外链接</el-radio>
            <el-radio :label="1">内链接</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="外部链接" prop v-if="radio == 0">
          <el-input v-model="form.hyperlink" placeholder="请输入链接" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType" v-if="radio == 1">
          <dict-select
            :options="dict.type.instation_skip"
            v-model="form.activityType"
            placeholder="请选择类型"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="类型列表" v-if="radio == 1">
          <el-select
            v-model="activityId"
            placeholder="请选择"
            @change="handleChange1"
            style="width:300px"
          >
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.name"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片地址" prop="cover">
          <image-upload :limit="1" v-model="form.cover"></image-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="广告标题" prop="title">
          <el-input v-model="form.title" type="text" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="外链" prop="">
          <el-radio v-model="form.type" label="0">外链接</el-radio>
          <el-radio v-model="form.type" label="1">内链接</el-radio>
           <el-radio-group v-model="type1">
            <el-radio :label="0">外链接</el-radio>
            <el-radio :label="1">内链接</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      
      
      <el-form ref="form" :model="form" :rules="rules">
         <el-form-item label="广告标题" prop="title">
          <el-input v-model="form.title" type="text" placeholder="请输入内容"/>
        </el-form-item>
         <el-form-item label="外链" prop="type">
          <el-radio v-model="form.type" label="0">外链接</el-radio>
          <el-radio v-model="form.type" label="1">内链接</el-radio>
        </el-form-item>

        <el-form-item label="外部链接" prop="" v-if="form.type == 0">
          <el-input v-model="form.hyperlink" placeholder="请输入链接" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType"  v-if="form.type == 1">
          <dict-select :options='dict.type.instation_skip' v-model="form.activityType" placeholder="请选择类型"
            @change="handleChange" />
        </el-form-item>
        <el-form-item label="类型列表" v-if="form.type == 1">
          <el-select v-model="activityId" placeholder="请选择" @change="handleChange1">
            <el-option v-for="(item, index) in typeList" :key="index" :label="item.name" :value="index">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片地址" prop="img">
          <image-upload :limit="1" v-model="form.img"></image-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('ok') }}</el-button>
        <el-button @click="cancel">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>-->
    <!-- 查看富文本 -->
    <el-dialog title="广告富文本" :visible.sync="openRichText" width="800px" append-to-body>
      <div v-html="richText"></div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdvertisement,
  getAdvertisement,
  delAdvertisement,
  addAdvertisement,
  // getAdvertisementModal,
  show
} from '@/api/advertisement/indexhAdvertisement'
import { typePage } from '@/api/setting/carousel'
import Template from '@/views/setting/template/index.vue'

export default {
  name: 'Advertisement',
  computed: {},
  components: { Template },
  dicts: ['instation_skip'],
  data () {
    return {
      radio: 0,
      // 添加修改类型列表分页参数
      queryFormPop: {
        pageNum: 1,
        pageSize: 999,
        id: null,
      },
      typeList: [],
      activityId: "",
      formModal: {},
      open2: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告表格数据
      advertisementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        type: 0,
      },
      type1: "0",
      // 表单校验
      rules: {
        title: { required: true, message: '广告标题不能为空', trigger: 'blur' },
        hyperlink: { required: true, message: '超链接不能为空', trigger: 'blur' },
        richText: { required: true, message: '富文本内容不能为空', trigger: 'blur' }
      },
      openRichText: false,
      richText: null,
    }
  },
  created () {
    this.getList()
    // this.getAdvertisementModal();
    this.form.type = 1
  },
  methods: {
    changeRadio (value) {
      this.form.type = value
    },
    handleChange (e) {
      this.queryFormPop.pageNum = 1
      this.activityId = null
      this.typeList = []
      this.queryTypeList(this.dict.type.instation_skip[e].raw.dictValue)
      console.log(this.form.activityId)
    },
    handleChange1 (e) {
      console.log(JSON.stringify(this.typeList[e]))
      this.activityId = this.typeList[e].name
      this.form.activityId = JSON.stringify(this.typeList[e])
      console.log(JSON.parse(JSON.stringify(this.typeList[e])).id)
    },
    // 查询内联类型列表
    async queryTypeList (id) {
      if (id) this.queryFormPop.id = id
      this.queryFormPop.pageSize = 999
      await typePage(this.queryFormPop).then(res => {
        this.typeList.push(...res.data.records)
        this.total1 = res.data.total
      })
    },
    /** 查询广告列表 */
    getList () {
      this.loading = true
      listAdvertisement(this.queryParams).then(response => {
        this.advertisementList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 广告弹窗
    // getAdvertisementModal(){
    //   getAdvertisementModal().then(response => { 
    //   })
    // },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {


      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 查看富文本
    handleRichText (row) {
      this.richText = row.richText
      this.openRichText = true
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.activityId = null
      this.reset()
      this.open = true
      this.title = '添加广告'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.form.type = row.type
      this.radio = Number(row.type)
      this.form.activityType = row.activityType
      if (row.activityId) {
        this.activityId = (JSON.parse(row.activityId)).name
      }
      this.queryFormPop.id = this.form.activityType
      // this.queryTypeList(this.queryFormPop.id)
      this.queryFormPop.pageSize = 999
      this.reset()
      const id = row.id || this.ids
      getAdvertisement(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改广告'
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.form.type = this.radio
      this.form.type = Number(this.form.type)
      this.form.activityType = Number(this.form.activityType)
      if (this.form.id) {
        this.form.id = Number(this.form.id)
      }
      console.log(this.form)
      this.$refs['form'].validate(valid => {
        if (valid) {
          delete this.form.updateBy
          delete this.form.updateTime
          addAdvertisement(this.form).then(response => {
            this.$modal.msgSuccess(this.form.id != null ? '修改成功' : '新增成功')
            this.getList()
            this.open = false
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除广告编号为"' + ids + '"的数据项？').then(function () {
        return delAdvertisement([ids])
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 立即弹窗 */
    handleShow (row) {
      this.open2 = true
      this.$modal.confirm('是否立即弹窗？').then(function () {
        return show(row.id)
      }).then(() => {
        // this.getList()
        this.$modal.msgSuccess('设置成功')
      }).catch(() => {
      })
    }
  }
}
</script>
